package com.qianye.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianye.entity.Customer;
import com.qianye.entity.Deleivertaff;
import com.qianye.entity.Expressinfo;
import com.qianye.entity.Role;
import com.qianye.mapper.CustomerMapper;
import com.qianye.mapper.DeleivertaffMapper;
import com.qianye.mapper.ExpressInfoMapper;
import com.qianye.mapper.RoleMapper;
import com.qianye.service.DaemonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/daemon")
public class DaemonController {

    @Autowired
    private DaemonService daemonService;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private DeleivertaffMapper deleivertaffMapper;

    @Autowired
    private ExpressInfoMapper expressInfoMapper;

    @RequestMapping("/expresslist")
    public String findAllExpress(@RequestParam(value = "express_owner_name",defaultValue = "") String express_owner_name,
                                 @RequestParam(value = "express_sender_name",defaultValue = "")String express_sender_name,
                                 @RequestParam(value = "express_owner_address",defaultValue = "") String express_owner_address,
                                 @RequestParam(value = "yname",defaultValue = "") String yname,
                                 @RequestParam(value = "yphone",defaultValue = "") String yphone,
                                 @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                 Model model) {
        PageHelper.startPage(pageNo,5);
        List<Expressinfo> expressinfoList = daemonService.findAllExpress(express_owner_name,express_sender_name,express_owner_address,yname,yphone);
        PageInfo<Expressinfo> pageInfo = new PageInfo<>(expressinfoList);
        model.addAttribute("expressinfoList",expressinfoList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("express_owner_name",express_owner_name);
        model.addAttribute("express_sender_name",express_sender_name);
        model.addAttribute("express_owner_address",express_owner_address);
        model.addAttribute("yname",yname);
        model.addAttribute("yphone",yphone);
        return "daemon/expresslist";
    }

    @RequestMapping("/expressreject")
    public String findAllExpressReject(Model model,@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo) {
        PageHelper.startPage(pageNo,5);
        List<Expressinfo> expressinfoList = daemonService.findAllExpressReject();
        PageInfo<Expressinfo> pageInfo = new PageInfo<>(expressinfoList);
        model.addAttribute("expressinfoList",expressinfoList);
        model.addAttribute("pageInfo",pageInfo);
        return "daemon/expressreject";
    }

    @RequestMapping("/customerlist")
    public String findAllCutomer(Model model,@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,@RequestParam(value = "cusername",defaultValue = "") String cusername,@RequestParam(value = "cidcard",defaultValue = "") String cidcard,@RequestParam(value = "cemail",defaultValue = "") String cemail,@RequestParam(value = "cphone",defaultValue = "") String cphone,@RequestParam(value = "caddress",defaultValue = "") String caddress) {
        PageHelper.startPage(pageNo,5);
        List<Customer> customerList = daemonService.findAllCustomer(cusername, cidcard, cemail, cphone, caddress);
        PageInfo<Customer> pageInfo = new PageInfo<>(customerList);
        model.addAttribute("customerList",customerList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("cusername",cusername);
        model.addAttribute("cidcard",cidcard);
        model.addAttribute("cemail",cemail);
        model.addAttribute("cphone",cphone);
        model.addAttribute("caddress",caddress);
        return "daemon/customerlist";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String deleteCustomer(@RequestParam("id") Integer id) {
        Customer customer = new Customer();
        customer.setCid(id);
        customer.setCstatus(2);
        customerMapper.updateById(customer);
        return "成功了";
    }

    @RequestMapping("/authorization")
    public String authorizationCostomer(@RequestParam("id") Integer id,Model model) {
        List<Role> roleList = daemonService.findAllRole();
        Customer customer = customerMapper.selectById(id);
        model.addAttribute("id",id);
        model.addAttribute("roleList",roleList);
        model.addAttribute("customer",customer);
        return "daemon/authorization";
    }

    @RequestMapping("/auth")
    @ResponseBody
    public String auth(Role role,Customer customer) {
        Integer rid = role.getRid();
        Role role1 = roleMapper.selectById(rid);
        role.setUid(customer.getCid());
        role.setRoleName(role1.getRoleName());
        role.setRoleDesc(role1.getRoleDesc());
        roleMapper.insert(role);
        return "成功修改";
    }

    @RequestMapping("/deliverlist")
    public String findAllDeliver(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,Model model) {
        PageHelper.startPage(pageNo,5);
        List<Deleivertaff> deleivertaffList = deleivertaffMapper.selectList(null);
        PageInfo<Deleivertaff> pageInfo = new PageInfo<>(deleivertaffList);
        model.addAttribute("deleivertaffList",deleivertaffList);
        model.addAttribute("pageInfo",pageInfo);
        return "daemon/deliverlist";

    }

    @RequestMapping("/deletedeliver")
    @ResponseBody
    public String deleteDeliverById(@RequestParam("id") Integer id) {
        deleivertaffMapper.deleteById(id);
        return "删除成功";
    }

    @RequestMapping("/deliveradd")
    @ResponseBody
    public String addDeliver(Deleivertaff deleivertaff) {
        deleivertaffMapper.insert(deleivertaff);
        return "添加成功";
    }

    @RequestMapping("/changestatus")
    public String changeStatus(@RequestParam("id") Integer id,Model model) {
        model.addAttribute("kid",id);
        return "daemon/changestatus";
    }

    @RequestMapping("/updatestatus")
    @ResponseBody
    public String updateStatus(Expressinfo expressinfo) {
        expressInfoMapper.updateById(expressinfo);
        return "更新成功了";
    }
}
